package com.bestchoice.myapp;

import imanager.IBrandsManager;
import imanager.IProductsManager;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import model.Products;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */

@Controller
public class HomeController {
	@Autowired
	IProductsManager productsManager;

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = { "/", "/Home" }, method = RequestMethod.GET)
	public String home(Locale locale, Model model, HttpServletRequest request) {

		int maxProductPerPage = 10;
		int page = 0;
		int Pages = 1;

		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		List<Products> listProducts = productsManager.getBetween(
				maxProductPerPage * page, maxProductPerPage * (page + 1));
		Pages = (int) Math.ceil(1.0 * productsManager.getAll().size()
				/ maxProductPerPage);

		request.setAttribute("listProducts", listProducts);
		request.setAttribute("page", page);
		request.setAttribute("Pages", Pages);
		// jgjgjgjgjgj
		return "home";
	}

}
